package cn.nexd.app.indoor.adapter;

import android.content.Context;
import android.graphics.PointF;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.map.rendering.core.componet.data.Feature;

/**
 * Created by wangxu on 16/3/9.
 */
public class SearchResultAdapter extends BaseAdapter {

    private Context context;
    private List<Feature> features;

    public SearchResultAdapter(Context context) {
        this.context = context;
        features = new ArrayList<>();
    }

    public void addFeatures(List<Feature> features) {
        this.features.clear();
        this.features.addAll(features);
        notifyDataSetChanged();
    }

    public List<PointF> getPointF() {

        ArrayList<PointF> pointFs = new ArrayList<>();
        for (Feature feature : features) {
            pointFs.add(new PointF(feature.getCenter_x(), feature.getCenter_y()));
        }

        return pointFs;
    }

    public void clear() {
        if (this.features != null) {
            this.features.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return features.size();
    }

    @Override
    public Feature getItem(int position) {
        return features.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, null);
            viewHolder.textView = (TextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(getItem(position).getName());
        viewHolder.textView.setPadding(10, 0, 10, 0);
        return convertView;
    }

    static class ViewHolder {
        TextView textView;
    }
}
